﻿using System;
using NguyenHoa.Services;
using NguyenHoa.Web.App_Code;

namespace NguyenHoa.Web.Controls
{
  public partial class SurveyCtrl : BaseControl
  {
    #region Properties
    private string CLASS_OPTION_CHOOSE = "choose";

    protected string SurveyQuestion
    {
      get
      {
        string question = string.Empty;
        Domains.Survey survey = GlobalCache.Instance.GetActiveSurvey(((BasePage)Page).CurrentLanguage.Id);
        if (survey != null)
          question = survey.Question;
        return question;
      }
    }

    private string m_surveyOption = string.Empty;
    protected string SurveyOptions
    {
      get
      {
        return m_surveyOption;
      }
    }

    #endregion

    #region Methods
    private void LoadSurvey()
    {
      Domains.Survey survey = GlobalCache.Instance.GetActiveSurvey(((BasePage)Page).CurrentLanguage.Id);
      if (survey == null || GlobalCache.Instance.SurveyOptions == null) return;
      m_surveyOption = string.Empty;
      string surveyType = survey.Type;
      if (surveyType == SurveyChoiceType.Single)
      {
        surveyType = "radio";
        hfIsSingleChoice.Value = "true";
      }
      else
      {
        surveyType = "checkbox";
        hfIsSingleChoice.Value = "false";
      }

      foreach (Domains.SurveyDetail option in GlobalCache.Instance.SurveyOptions)
      {
        string rdoId = "rad" + option.Id;
        m_surveyOption += "<div class='" + CLASS_OPTION_CHOOSE + "'>";
        m_surveyOption += "<label for='" + rdoId + "'>";
        m_surveyOption += "<input name='option' type='" + surveyType + "' value='" + option.Id + "' id='" + rdoId + "' ";
        m_surveyOption += "onclick=\"tickSurvey('" + rdoId + "', this.checked);\" />";
        m_surveyOption += option.Name;
        m_surveyOption += "</label>";
        m_surveyOption += "</div>";
      }
    }

    private string SaveSurvey()
    {
      try
      {
        using (ConnectionScope.CreateTransaction())
        {
          SurveyDetailService service = new SurveyDetailService();
          string[] choiceList = hfSurveyCheckbox.Value.Split(';');
          for (int i = 0; i < choiceList.Length; i++)
          {
            if (string.IsNullOrEmpty(choiceList[i]) || choiceList[i].Length < 5)
              continue;

            int choiceId = 0;
            int.TryParse(choiceList[i].Substring(4), out choiceId);
            service.IncreaseValue(choiceId);
          }

          ConnectionScope.Complete();
        }
      }
      catch (Exception ex)
      {
        WriteLog("SurveyCtrl.ascx::SaveSurvey", ex);
        return Resources.Messages.ERR0036;
      }
      return string.Empty;
    }
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!Page.IsCallback && !Page.IsPostBack)
      {
        LoadSurvey();
      }
    }

    protected void surveyCallback_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
    {
      string[] para = e.Parameter.Split('|');
      if (para[0] == "Survey")
      {
        e.Result = SaveSurvey();
      }
    }
    #endregion
  }
}